<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
    <el-col>
      <strong>
        <i v-if="isRoot" class="el-icon-office-building" /> {{ nodeData.name }}</strong>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span class="optBtn">
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepartment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDepartment">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepartment">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 删除部门
    async delDepartment() {
      try {
        await this.$confirm('是否确认删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 删除请求
        await delDepartment(this.nodeData.id)
        this.$message.success('删除部门成功')
        // 通知到父组件进行更新页面
        this.$emit('delDepartment')
      } catch (err) {
        console.log(err)
      }
    },
    // 点击添加子部门，子组件向父组件传递
    addDepartment() {
      // 弹框，同时也要把是谁点击的也传过去。
      this.$emit('addDepartment', this.nodeData)
    },
    // 编辑弹出框
    editDepartment() {
      // 通知父页面弹窗
      this.$emit('editDepartment', this.nodeData)
    }
  }
}
</script>

<style>

</style>
